package com.zhuoyue.biz.core.api.service.impl;

import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.zhuoyue.biz.core.api.domain.EavizVerificationcodeDO;
import com.zhuoyue.common.exception.ErrorType;
import com.zhuoyue.common.exception.ZyServerException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zhuoyue.biz.core.api.dao.EavizVerificationcodeMapper;
import com.zhuoyue.biz.core.api.domain.EavizVerificationcode;

/**
 * @author wjming
 * @ClassName: EavizVerificationcodeService
 * @Description:萤石验证码服务
 * @Date 2021-06-24 17:34
 */
@Service
public class EavizVerificationcodeService extends ServiceImpl<EavizVerificationcodeMapper, EavizVerificationcode> {
    @Autowired
    private EavizVerificationcodeMapper eavizVerificationcodeMapper;

    /**
     * 查询
     */
    public String getVerifiCationCode(EavizVerificationcodeDO query) {
        return eavizVerificationcodeMapper.selectVerifiCationCode(query.getSerialNum());
    }

    /**
     * 修改验证码
     *
     * @param query
     * @return
     */
    public Boolean updateVerifiCationCode(EavizVerificationcodeDO query) {
        EavizVerificationcode eavizVerificationcode = EavizVerificationcode.builder().homeId(query.getHomeId())
                .serialNum(query.getSerialNum()).verificationCode(query.getVerificationCode()).build();
        boolean b = saveOrUpdate(eavizVerificationcode, Wrappers.<EavizVerificationcode>lambdaQuery()
                .eq(EavizVerificationcode::getSerialNum, query.getSerialNum()));
        if (!b) {
            throw new ZyServerException(ErrorType.Service_Fail);
        }
        return true;
    }

    /**
     * 通过验证码删除
     *
     * @param serialNum
     */
    public void remove(String serialNum) {
        int count = eavizVerificationcodeMapper.delete(Wrappers.<EavizVerificationcode>lambdaQuery()
                .eq(EavizVerificationcode::getSerialNum, serialNum));
        if (count < 1) {
            throw new ZyServerException(ErrorType.Service_Fail);
        }
    }

}
